package classLoader;

import java.io.*;

public class MyClassLoader extends ClassLoader {

	private String path;

	public MyClassLoader() {
	}
	public MyClassLoader(String path) {
		this.path = path;
	}

	@Override
	public Class<?> findClass(String name) {
		byte[] bytes = loadDataBytes(name);
		return defineClass(name, bytes, 0, bytes.length);
	}

	private byte[] loadDataBytes(String name) {
		name = path + name + ".class";
		try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(name));
		     ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
			byte[] bytes = new byte[1000];
			int len;
			while ((len = bis.read(bytes)) != -1) {
				bos.write(bytes, 0, len);
			}
			return bos.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
